function intersection(arr1, arr2) {
  const set1 = new Set(arr1);
  const set2 = new Set(arr2);
  const intersectionSet = new Set([...set1].filter((x) => set2.has(x)));
  return [...intersectionSet];
}
// 多个数组的交集
function multiIntersection(...arrays) {
  if (arrays.length === 0) return [];
  if (arrays.length === 1) return arrays[0];

  const [first, ...rest] = arrays;
  return intersection(first, multiIntersection(...rest));
}

function difference(arr1, arr2) {
  const set1 = new Set(arr1);
  const set2 = new Set(arr2);
  const differenceSet = new Set([...set1].filter((x) => !set2.has(x)));
  return [...differenceSet];
}
function multiDifference(arr, ...rest) {
  if (rest.length === 0) return arr;
  return difference(arr, multiDifference(...rest));
}

function approximationAlgorithm() {
  // 要覆盖州的集合
  let states_needed = ["mt", "wa", "or", "id", "nv", "ut", "ca", "az"];
  // 可供选择的广播电台清单,其中的键为广播台的名称，值为广播台覆盖的州
  let stations = {
    kone: ["id", "nv", "ut"],
    ktwo: ["wa", "id", "mt"],
    kthree: ["or", "nv", "ca"],
    kfour: ["nv", "ut"],
    kfive: ["ca", "az"],
  };
  // 存储最终选择的广播台
  let final_stations = [];

  // 遍历所有的广播台，从中选择覆盖了最多的未覆盖州的广播台
  let best_station = [];
 
  let states_covered = [];
  for (let [station, states_for_station] of Object.entries(stations)) {
     // 并集- 同时出现在states_needed和states_for_station中的州；
    let covered = multiIntersection(states_needed, states_for_station);
    // 检查该广播台覆盖的州是否比best_station多。
    if (covered.length > states_covered.length) {
      best_station = station;
      states_covered = covered;
    }
  }
  final_stations=best_station
}

approximationAlgorithm();
